package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions

import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityBianjizhuanquBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import com.google.android.material.bottomsheet.BottomSheetDialog
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import yja.gzw.app.R
import java.io.File

class BianjizhuanquActivity :BaseActivity<Mainviewmodel, ActivityBianjizhuanquBinding>(R.layout.activity_bianjizhuanqu){


        private lateinit var bottomSheetDialog: BottomSheetDialog
        var shuzi=1
        var file: File?=null
        var pingtai=""


        override fun onResume() {
            super.onResume()

            binding.weizhixingxi.setText(UserData.diming)

        }
        private fun openGallery() {
            val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(intent, GALLERY_REQUEST_CODE)
        }

        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                data?.data?.let { uri ->
                    file = File(getRealPathFromURI(uri))

                    val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                    val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                    viewmodel.onluying(body)
                }
            }
        }

        private fun getRealPathFromURI(contentUri: Uri): String {
            val projection = arrayOf(MediaStore.Images.Media.DATA)
            val cursor = contentResolver.query(contentUri, projection, null, null, null)
            val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
            cursor?.moveToFirst()
            val result = cursor?.getString(column_index!!)
            cursor?.close()
            return result!!
        }
        companion object {
            private const val GALLERY_REQUEST_CODE = 1001
        }



        override fun initView() {


            val getid =intent.getStringExtra("id")

                Log.e("传过来的id",getid!!)

            binding.shipingsucai.setOnClickListener {
                openGallery()


            }

            binding.fabupingtai.setOnClickListener {
                bottomSheetDialog = BottomSheetDialog(this)
                val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_pingtia, null)
                bottomSheetDialog.setContentView(view)
                val xuan1 =view.findViewById<CheckBox>(R.id.xuan1)
                val xuan2 =view.findViewById<CheckBox>(R.id.xuan2)
                val xuan3 =view.findViewById<CheckBox>(R.id.xuan3)
                val xuan4 =view.findViewById<CheckBox>(R.id.xuan4)
                val xuan5 =view.findViewById<CheckBox>(R.id.xuan5)
                val queding =view.findViewById<TextView>(R.id.queding)
                val quxiao =view.findViewById<TextView>(R.id.quxiao)
                val chacha =view.findViewById<ImageView>(R.id.chacha)

                chacha.setOnClickListener {
                    bottomSheetDialog.dismiss()

                }
                quxiao.setOnClickListener {
                    bottomSheetDialog.dismiss()
                }
                queding.setOnClickListener {



                    if(xuan1.isChecked){
                        pingtai+="抖音,"

                    }else if(xuan2.isChecked){
                        pingtai+="快手,"

                    }else if(xuan3.isChecked){
                        pingtai+="腾讯微视,"

                    }else if(xuan4.isChecked){
                        pingtai+="西瓜视频,"

                    }else if(xuan5.isChecked){

                        pingtai+="bilibili,"

                    }


                    bottomSheetDialog.dismiss()
                    binding.pingtai.text.toString()
                }


                bottomSheetDialog.show()


            }



            binding.aishipinggzt.setOnClickListener {
                val inten= Intent(this, AishipinggztActivity::class.java)
                startActivity(inten)
            }
            binding.shipingmoshi.setOnClickListener {
                bottomSheetDialog = BottomSheetDialog(this)
                val view = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_moshi, null)
                bottomSheetDialog.setContentView(view)
                val okButton2 =view.findViewById<TextView>(R.id.queding)
                val chahao =view.findViewById<ImageView>(R.id.chacha)
                val quxiao =view.findViewById<TextView>(R.id.quxiao)
                val xuan1 =view.findViewById<RadioButton>(R.id.xuan1)
                val xuan2 =view.findViewById<RadioButton>(R.id.xuan2)
                val xuan3 =view.findViewById<RadioButton>(R.id.xuan3)


                okButton2.setOnClickListener {



                    if(xuan1.isChecked){

                        binding.aishipinggzt.isVisible=true
                        binding.aishipinggzt.text = "AI视频工作台"
                        binding.bushiyongai.isVisible=false
                    } else if(xuan2.isChecked){
                        binding.bushiyongai.isVisible=false
                        binding.aishipinggzt.isVisible=true
                        binding.aishipinggzt.text = "填写AI生成视频素材"

                    }else{
                        binding.aishipinggzt.isVisible=false
                        binding.bushiyongai.isVisible=true
                    }

                    bottomSheetDialog.dismiss()


                }
                chahao.setOnClickListener {
                    bottomSheetDialog.dismiss()
                }
                quxiao.setOnClickListener {

                    bottomSheetDialog.dismiss()
                }

                bottomSheetDialog.show()



            }

            binding.dinwei.setOnClickListener {
                val inten= Intent(this, MapsActivity::class.java)
                startActivity(inten)

            }

            binding.jian.setOnClickListener {
                shuzi--
                binding.shuzi.text=shuzi.toString()


            }
            binding.jia.setOnClickListener {
                shuzi++
                binding.shuzi.text=shuzi.toString()

            }



            binding.fabuanniu.setOnClickListener {
                val biaoti=binding.biaotizhan.text.toString()
                val pintia=binding.pingtai.text.toString()
                val biaotinr=binding.biaotinr.text.toString()
                val scfs=binding.scfs.text.toString()
                val shipinneirun=binding.shipingneirun.text.toString()
                val xuqiubiaoti=binding.xuqiubiaoti.text.toString()

                if(UserData.wengbengsucai==""){

                    UserData.wengbengsucai=binding.shurukuangneirung.text.toString()
                }



                viewmodel.onbianjidingdanwang(
                    UserData.token, mapOf("platform_id" to pintia
                    ,"title" to biaotinr,"order_id" to getid,"release_type" to scfs,"release_num" to shuzi.toString(),
                    "address" to UserData.diming,"label" to biaoti,"video_url" to UserData.hechengdeshipinglianjie,
                    "content" to shipinneirun))
            }
        }

        override fun dataObservese() {


            viewmodel.onlusc.observe(this, Observer {
                if(it.code==1){

                    val shipinglujin= "http://117ai.32.hzgqapp.com"+it.data.url

                    UserData.hechengdeshipinglianjie=shipinglujin
                    Log.e("视频上传的路径", shipinglujin)

                    Glide.with(this)
                        .setDefaultRequestOptions(
                            RequestOptions()
                                .frame(0)
                                .centerCrop()
                        )
                        .load(shipinglujin)
                        .into(binding.shipingsucai)

                }


            })


            viewmodel.onZhifushow.observe(this, Observer {
                if(it.code==1){
                    finish()
                    bottomSheetDialog.dismiss()
                    UserData.wengbengsucai=""
                    Toast.makeText(this, it.msg, Toast.LENGTH_SHORT).show()

                }else{
                    Toast.makeText(this, it.msg, Toast.LENGTH_SHORT).show()

                }

            })


            viewmodel.onbianjidingdan.observe(this, Observer {
                if(it.code==1){
                    Log.e("编辑状态","走到了")
                    finish()
                    Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()


                }else{
                    Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

                }

            })

        }

}